﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity;
using System;
using System.Windows;
using System.Windows.Input;

namespace HobSoft.SoccerBet2014.Common.Wpf.Behavior
{
    public static class MouseClick
    {
        private static readonly DependencyProperty ClickCommandBehaviorProperty =
            DependencyProperty.RegisterAttached("ClickCommandBehavior", typeof(ClickCommandBehavior), typeof(MouseClick), null);

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(MouseClick), new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(MouseClick), new PropertyMetadata(OnSetCommandParameterCallback));

        public static void SetCommand(UIElement control, ICommand command)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            control.SetValue(MouseClick.CommandProperty, command);
        }

        public static ICommand GetCommand(UIElement control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            return control.GetValue(MouseClick.CommandProperty) as ICommand;
        }

        public static void SetCommandParameter(UIElement control, object parameter)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            control.SetValue(MouseClick.CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(UIElement control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            return control.GetValue(MouseClick.CommandParameterProperty);
        }

        private static ClickCommandBehavior GetOrCreateBehavior(UIElement control)
        {
            ClickCommandBehavior clickCommandBehavior = control.GetValue(MouseClick.ClickCommandBehaviorProperty) as ClickCommandBehavior;
            if (clickCommandBehavior == null)
            {
                clickCommandBehavior = new ClickCommandBehavior(control);
                control.SetValue(MouseClick.ClickCommandBehaviorProperty, clickCommandBehavior);
            }
            return clickCommandBehavior;
        }


        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as UIElement;
            if (control == null) return;
            var behavior = GetOrCreateBehavior(control);
            behavior.Command = e.NewValue as ICommand;
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as UIElement;
            if (control == null) return;
            var behavior = GetOrCreateBehavior(control);
            behavior.CommandParameter = e.NewValue;
        }
    }

    public class ClickCommandBehavior : CommandBehaviorBase<UIElement>
    {
        public ClickCommandBehavior(UIElement obj)
            : base(obj)
        {
            obj.PreviewMouseDown += OnMouseDown;
        }

        void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            ExecuteCommand(this.CommandParameter);
        }
    }
}
